package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Repository;

import pojo.Customer;
import util.DBUtils;
import dao.CustomerDao;

@Repository(value="customer")
public class CustomerDaoImpl implements CustomerDao {
	private Connection con;
	@Resource
	private DataSource dataSource1;
	public CustomerDaoImpl() {

	}

	@Override
	public Customer addCustomer(Customer c) {
		
		String sql = "insert into customerList(id, cname, sal, sex, birth) values(?,?,?,?,?)";
		PreparedStatement pst = null;
		try {
			con = dataSource1.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		int m = 0;
		try {
//			con = DBUtils.createConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, c.getId());
			pst.setString(2, c.getCname());
			pst.setDouble(3, c.getSal());
			pst.setBoolean(4, c.isSex());
			pst.setDate(5, new java.sql.Date(c.getBirth().getTime()));
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		finally {
			DBUtils.close(con, pst, null);
		}
		return c;
	}

	@Override
	public Customer updateCustomer(Customer c) {
		String sql = "update customerList set cname=?, sal=?, sex=?, birth=? where id=?";
//		Connection con=null;
		PreparedStatement pst = null;
		try {
			con = dataSource1.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		int m = 0;
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, c.getCname());
			pst.setDouble(2, c.getSal());
			pst.setBoolean(3, c.isSex());
			pst.setDate(4, new java.sql.Date(c.getBirth().getTime()));
			pst.setString(5, c.getId());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		finally {
			DBUtils.close(con, pst, null);
		}
		return c;		
	}

	@Override
	public List<Customer> findAllCustomers() {
		List<Customer>list = new ArrayList<Customer>();
		String sql = "select * from customerList";
		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			con = dataSource1.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		Customer c = null;
		try {
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()){
				c = new Customer();
				c.setId(rs.getString("id"));
				c.setCname(rs.getString("cname"));
				c.setSal(rs.getDouble("sal"));
				c.setSex(rs.getBoolean("sex"));
				c.setBirth(new Date(rs.getDate("birth").getTime()));
				list.add(c);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		finally {
			DBUtils.close(con, pst, null);
		}
		return list;
	}

	@Override
	public String deleteCustomer(String id) {
		String sql = "delete customerList where id=?";
//		Connection con=null;
		PreparedStatement pst = null;
		try {
			con = dataSource1.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		int m = 0;
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, id);
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		finally {
			DBUtils.close(con, pst, null);
		}
		return null;
	}

}
